<?php
namespace app\serversetting\logic;

use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-06-21 14:08:39
 * @Description: 全局设置 配置分类
 */
class SettingCategory extends BaseLogic
{
	protected $name = 'setting_category';
    protected $config = 'db_config_setting';
    protected $settingCategoryLogic = '\app\serversetting\logic\SettingCategory';
    protected $settingCategoryModel = '\app\serversetting\model\SettingCategory';
    protected $allowField = ['other_app_read','name','action','order','is_public'];

    use \app\serveruser\traits\addAppId;
    use \app\servergoods\traits\UpdateCate;
    use \app\servergoods\traits\GetCateTree;
    /*内部获取某个分类的详情*/
    public function dbCateInfo($data) 
    {
    	return $this->db()->where(['id'=>$data['id'],'status'=>['neq','-1']])->find();
    }

    /*根据id获取其父类的ids数组*/
    public function getParentIds($id): array 
    {
        $list = $this->db()->where(['status'=>['neq',-1]])->select();
        $ids     = $id;
        $params  = 'id';
        $parentIds = parent_ids($list,$ids,$params,$id,0,'pid','id','');
        $ids = explode(",", $ids);
        foreach ($ids as $k => &$v) {
            $v = (int)$v;
        }
        array_splice($ids, array_search(0, $ids), 1);
        return array_reverse($ids);
    }

    /*内部获取不包含公共分类的所有分类列表 查询结果可用于操作*/
    public function dbCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        if(isset($map['app_id'])){
            $isManageAppId = $this->_isManageAppId($map['app_id']);
            !$isManageAppId && $_where['app_id'] = $map['app_id'];
        }
        $_where['status'] = ['neq',-1];
        return $this->db()->where($_where)->select();
    }

    /*内部获取包含公共分类的所有分类列表  仅查询不用于操作*/
    public function dbAllCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        $_where['status'] = ['neq',-1];
        if(isset($map['app_id'])){
            $isManageAppId = $this->_isManageAppId($map['app_id']);
            /*$_whereOr = $isManageAppId ? true : "`app_id` = {$map['app_id']} OR FIND_IN_SET({$map['app_id']},`other_app_read`) OR is_public = 1";*/
            $_whereOr = $isManageAppId ? true : '`app_id`=' . $map['app_id'] . ' OR FIND_IN_SET(' . $map['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }
        $res = $this->db()->where($_where)->where($_whereOr)->select();
        return $res;
    }

    /*添加一个分类*/
    public function addCategory($data): array 
    {
    	$res = $this->model($this->settingCategoryModel)->save($data);
    	return auto_return($res, '添加分类成功', '添加分类失败');
    }
    
    /*编辑一个分类*/
    public function editCategory($data): array 
    {
        return $this->_updateCate($this->settingCategoryLogic, $this->settingCategoryModel, $data, 'dbCateList', 'dbCateInfo');
    }

    /*获取本应用下的分类树 包含公共分类*/
    public function getTree($data): array 
    {
        return $this->_getCateTree($data, $this->settingCategoryLogic, 'dbAllCateList');
    }

    /*获取某分类详情*/
    public function oneCateInfo($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['status'] = ['neq',-1];
        if(isset($data['app_id'])){   //若卖家查看商品详情，则不传入app_id
            $manageAppId = \app\manage\service\ApiBase::getAppId();
            $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }
        $res = $this->model($this->settingCategoryModel)->where($_where)->where($_whereOr)->find();
        return auto_return($res);
    }

    /*批量删除分类*/
    public function batchDelCate($data): array 
    {
        $_where['id'] = ['in', $data['id']];
        $_where['status'] = ['neq', '-1'];
        $_where = $this->_addAppId($_where, $data['app_id']);
        $res = $this->db()->where($_where)->update(['status'=>'-1']);
        return auto_return($res);
    }

    //批量启用/禁用选择的分类
    public function batchChangeStatus($data): array
    {
        $list = $this->dbCateList();
        $saveData['status'] = $data['status'];

        is_numeric($data['id']) && $data['id'] = [$data['id']];
        foreach ($data['id'] as $k => $v) {
            $childId = child_ids($list, $v, 'id', 'pid', 'id');  //使用static数组 获取该分类下的所有子类
        }
        $data['id'] = array_merge($childId, $data['id']);
        $_where['id'] = ['in',$data['id']];
        $_where = $this->_addAppId($_where, $data['app_id']);
        
        $res = $this->model($this->settingCategoryModel)->save($saveData, $_where);
        return auto_return($res);
    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array 
    {
        $info = $this->db()->where(['action'=>$data['action']])->find();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }
 



}